package com.example.texted.templatemethod;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

abstract class AbsPopulator {
    //模板方法
    public final void dataInitialing() throws InvocationTargetException, IllegalAccessException {
        //获得所有的public方法
        Method[] methods = getClass().getMethods();
        for (Method method : methods) {
            System.out.println(method.getDeclaringClass()+"  "+ method.getName());
            if (isInitDataMethod(method)){
                method.invoke(this);
            }
        }
    }

    private boolean isInitDataMethod(Method method) {
        return method.getName().startsWith("init")
                && Modifier.isPublic(method.getModifiers())
                && method.getReturnType().equals(Void.TYPE)
                && !method.isVarArgs()
                && !Modifier.isAbstract(method.getModifiers());
    }
}

